package com.loong.musicdemo.ui.playsong;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.loong.musicdemo.R;
import com.loong.musicdemo.adapter.PageSongListAdapter;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.ui.mine.adapter.SongAdapter;
import com.loong.musicdemo.util.dataDeal.MusicDeal;
import com.loong.musicdemo.util.dataDeal.MvDeal;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * 历史记录列表,
 * 默认播放列表以及每次播放添加的歌曲
 * 记录在缓存中
 */
public class PageHistoryFragment extends Fragment implements View.OnClickListener {
    private List<Song> historyList = new ArrayList<>();//历史记录列表
    private PageSongListAdapter adapter;//适配器

    public PageHistoryFragment() {
        //无参构造器
    }

    public static PageHistoryFragment newInstance() {
        return new PageHistoryFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取数据库信息
        initHistory();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_page_history, container, false);
        root.findViewById(R.id.play_current).setOnClickListener(this);
        root.findViewById(R.id.delete_current).setOnClickListener(this);
        RecyclerView recyclerHistoryList = root.findViewById(R.id.current_list_recycler);
        adapter = new PageSongListAdapter(historyList);
        //设置点击事件等
        adapter.setOnClickSongItem(new PageSongListAdapter.OnClickSongItem() {
            @Override
            public void onDeleteClick(int position) {
                //删除子项
                deleteDataById(historyList.get(position).getSongId());
                historyList.remove(position);//删除子项
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onSongClick(int position) {
                //播放歌曲

            }
        });
        recyclerHistoryList.setAdapter(adapter);
        return root;
    }

    /**
     * 删除id的数据库历史记录
     *
     * @param songId 歌曲id
     */
    private void deleteDataById(String songId) {
        //删除数据库信息

        //删除本地音乐文件
    }

    /**
     * 从数据库中获取播放历史记录
     */
    private void initHistory() {
        new RecordTask().execute();//初始化数据
    }

    /**
     * 异步任务请求与用户有关的最近播放音乐的id
     * 获取最近播放的音乐
     * 获取最近播放中的mvIds
     */
    @SuppressLint("StaticFieldLeak")
    class RecordTask extends AsyncTask<String, Void, List<Song>> {
        @Override
        protected List<Song> doInBackground(String... strings) {
            SharedPreferences userInfo = getContext().getSharedPreferences("userInfo", MODE_PRIVATE);
            String userId = userInfo.getString("userId", null);
            //获取用户最近播放音乐的id
            List<String> ids = MusicDeal.getWeekPlayRecordIds(userId);
            return MusicDeal.getSongListByIds(ids);

        }

        @Override
        protected void onPostExecute(List<Song> songs) {
            super.onPostExecute(songs);
            historyList = songs;
            adapter.notifyDataSetChanged();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play_current:
            case R.id.delete_current:
            default:
        }
    }
}